home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / remote-eb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  22.7 KB  |  941 lines

  1. /* Remote debugging interface for AMD 29000 EBMON on IBM PC, for GDB.
  2.    Copyright 1990-1991 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.  Written by Jim Kingdon for Cygnus.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This is like remote.c but is for an esoteric situation--
  22.    having a 29k board in a PC hooked up to a unix machine with
  23.    a serial line, and running ctty com1 on the PC, through which
  24.    the unix machine can run ebmon.  Not to mention that the PC
  25.    has PC/NFS, so it can access the same executables that gdb can,
  26.    over the net in real time.  */
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include "defs.h"
  31. #include "tm-29k.h"
  32. #include "param-no-tm.h"
  33. #include "inferior.h"
  34. #include "wait.h"
  35. #include "value.h"
  36. #include <ctype.h>
  37. #include <fcntl.h>
  38. #include <signal.h>
  39. #include <errno.h>
  40. #include "terminal.h"
  41. #include "target.h"
  42.  
  43. extern struct value *call_function_by_hand();
  44.  
  45. extern struct target_ops eb_ops;        /* Forward declaration */
  46.  
  47. #define LOG_FILE "eb.log"
  48. #if defined (LOG_FILE)
  49. FILE *log_file;
  50. #endif
  51.  
  52. static int timeout = 5;
  53.  
  54. /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
  55.    eb_open knows that we don't have a file open when the program
  56.    starts.  */
  57. int eb_desc = -1;
  58.  
  59. /* stream which is fdopen'd from eb_desc.  Only valid when
  60.    eb_desc != -1.  */
  61. FILE *eb_stream;
  62.  
  63. /* Read a character from the remote system, doing all the fancy
  64.    timeout stuff.  */
  65. static int
  66. readchar ()
  67. {
  68.   char buf;
  69.  
  70.   buf = '\0';
  71. #ifdef HAVE_TERMIO
  72.   /* termio does the timeout for us.  */
  73.   read (eb_desc, &buf, 1);
  74. #else
  75.   alarm (timeout);
  76.   if (read (eb_desc, &buf, 1) < 0)
  77.     {
  78.       if (errno == EINTR)
  79.     error ("Timeout reading from remote system.");
  80.       else
  81.     perror_with_name ("remote");
  82.     }
  83.   alarm (0);
  84. #endif
  85.  
  86.   if (buf == '\0')
  87.     error ("Timeout reading from remote system.");
  88. #if defined (LOG_FILE)
  89.   putc (buf & 0x7f, log_file);
  90. #endif
  91.   return buf & 0x7f;
  92. }
  93.  
  94. /* Keep discarding input from the remote system, until STRING is found. 
  95.    Let the user break out immediately.  */
  96. static void
  97. expect (string)
  98.      char *string;
  99. {
  100.   char *p = string;
  101.  
  102.   immediate_quit = 1;
  103.   while (1)
  104.     {
  105.       if (readchar() == *p)
  106.     {
  107.       p++;
  108.       if (*p == '\0')
  109.         {
  110.           immediate_quit = 0;
  111.           return;
  112.         }
  113.     }
  114.       else
  115.     p = string;
  116.     }
  117. }
  118.  
  119. /* Keep discarding input until we see the ebmon prompt.
  120.  
  121.    The convention for dealing with the prompt is that you
  122.    o give your command
  123.    o *then* wait for the prompt.
  124.  
  125.    Thus the last thing that a procedure does with the serial line
  126.    will be an expect_prompt().  Exception:  eb_resume does not
  127.    wait for the prompt, because the terminal is being handed over
  128.    to the inferior.  However, the next thing which happens after that
  129.    is a eb_wait which does wait for the prompt.
  130.    Note that this includes abnormal exit, e.g. error().  This is
  131.    necessary to prevent getting into states from which we can't
  132.    recover.  */
  133. static void
  134. expect_prompt ()
  135. {
  136. #if defined (LOG_FILE)
  137.   /* This is a convenient place to do this.  The idea is to do it often
  138.      enough that we never lose much data if we terminate abnormally.  */
  139.   fflush (log_file);
  140. #endif
  141.   expect ("\n# ");
  142. }
  143.  
  144. /* Get a hex digit from the remote system & return its value.
  145.    If ignore_space is nonzero, ignore spaces (not newline, tab, etc).  */
  146. static int
  147. get_hex_digit (ignore_space)
  148.      int ignore_space;
  149. {
  150.   int ch;
  151.   while (1)
  152.     {
  153.       ch = readchar ();
  154.       if (ch >= '0' && ch <= '9')
  155.     return ch - '0';
  156.       else if (ch >= 'A' && ch <= 'F')
  157.     return ch - 'A' + 10;
  158.       else if (ch >= 'a' && ch <= 'f')
  159.     return ch - 'a' + 10;
  160.       else if (ch == ' ' && ignore_space)
  161.     ;
  162.       else
  163.     {
  164.       expect_prompt ();
  165.       error ("Invalid hex digit from remote system.");
  166.     }
  167.     }
  168. }
  169.  
  170. /* Get a byte from eb_desc and put it in *BYT.  Accept any number
  171.    leading spaces.  */
  172. static void
  173. get_hex_byte (byt)
  174.      char *byt;
  175. {
  176.   int val;
  177.  
  178.   val = get_hex_digit (1) << 4;
  179.   val |= get_hex_digit (0);
  180.   *byt = val;
  181. }
  182.  
  183. /* Get N 32-bit words from remote, each preceded by a space,
  184.    and put them in registers starting at REGNO.  */
  185. static void
  186. get_hex_regs (n, regno)
  187.      int n;
  188.      int regno;
  189. {
  190.   long val;
  191.   int i;
  192.  
  193.   for (i = 0; i < n; i++)
  194.     {
  195.       int j;
  196.       
  197.       val = 0;
  198.       for (j = 0; j < 8; j++)
  199.     val = (val << 4) + get_hex_digit (j == 0);
  200.       supply_register (regno++, &val);
  201.     }
  202. }
  203.  
  204. /* Called when SIGALRM signal sent due to alarm() timeout.  */
  205. #ifndef HAVE_TERMIO
  206.  
  207. #ifndef __STDC__
  208. #define volatile /**/
  209. #endif
  210. volatile int n_alarms;
  211.  
  212. void
  213. eb_timer ()
  214. {
  215. #if 0
  216.   if (kiodebug)
  217.     printf ("eb_timer called\n");
  218. #endif
  219.   n_alarms++;
  220. }
  221. #endif
  222.  
  223. /* malloc'd name of the program on the remote system.  */
  224. static char *prog_name = NULL;
  225.  
  226. /* Nonzero if we have loaded the file ("yc") and not yet issued a "gi"
  227.    command.  "gi" is supposed to happen exactly once for each "yc".  */
  228. static int need_gi = 0;
  229.  
  230. /* Number of SIGTRAPs we need to simulate.  That is, the next
  231.    NEED_ARTIFICIAL_TRAP calls to eb_wait should just return
  232.    SIGTRAP without actually waiting for anything.  */
  233.  
  234. static int need_artificial_trap = 0;
  235.  
  236. /* This is called not only when we first attach, but also when the
  237.    user types "run" after having attached.  */
  238. void
  239. eb_start (inferior_args)
  240. char *inferior_args;
  241. {
  242.   /* OK, now read in the file.  Y=read, C=COFF, D=no symbols
  243.      0=start address, %s=filename.  */
  244.  
  245.   fprintf (eb_stream, "YC D,0:%s", prog_name);
  246.  
  247.   if (inferior_args != NULL)
  248.       fprintf(eb_stream, " %s", inferior_args);
  249.  
  250.   fprintf (eb_stream, "\n");
  251.   fflush (eb_stream);
  252.  
  253.   expect_prompt ();
  254.  
  255.   need_gi = 1;
  256. }
  257.  
  258. /* Translate baud rates from integers to damn B_codes.  Unix should
  259.    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
  260.  
  261. #ifndef B19200
  262. #define B19200 EXTA
  263. #endif
  264. #ifndef B38400
  265. #define B38400 EXTB
  266. #endif
  267.  
  268. struct {int rate, damn_b;} baudtab[] = {
  269.     {0, B0},
  270.     {50, B50},
  271.     {75, B75},
  272.     {110, B110},
  273.     {134, B134},
  274.     {150, B150},
  275.     {200, B200},
  276.     {300, B300},
  277.     {600, B600},
  278.     {1200, B1200},
  279.     {1800, B1800},
  280.     {2400, B2400},
  281.     {4800, B4800},
  282.     {9600, B9600},
  283.     {19200, B19200},
  284.     {38400, B38400},
  285.     {-1, -1},
  286. };
  287.  
  288. int damn_b (rate)
  289.      int rate;
  290. {
  291.   int i;
  292.  
  293.   for (i = 0; baudtab[i].rate != -1; i++)
  294.     if (rate == baudtab[i].rate) return baudtab[i].damn_b;
  295.   return B38400;    /* Random */
  296. }
  297.  
  298.  
  299. /* Open a connection to a remote debugger.
  300.    NAME is the filename used for communication, then a space,
  301.    then the name of the program as we should name it to EBMON.  */
  302.  
  303. static int baudrate = 9600;
  304. static char *dev_name;
  305. void
  306. eb_open (name, from_tty)
  307.      char *name;
  308.      int from_tty;
  309. {
  310.   TERMINAL sg;
  311.  
  312.   char *p;
  313.  
  314.   target_preopen (from_tty);
  315.   
  316.   /* Find the first whitespace character, it separates dev_name from
  317.      prog_name.  */
  318.   if (name == 0)
  319.     goto erroid;
  320.  
  321.   for (p = name;
  322.        *p != '\0' && !isspace (*p); p++)
  323.     ;
  324.   if (*p == '\0')
  325. erroid:
  326.     error ("\
  327. Please include the name of the device for the serial port,\n\
  328. the baud rate, and the name of the program to run on the remote system.");
  329.   dev_name = alloca (p - name + 1);
  330.   strncpy (dev_name, name, p - name);
  331.   dev_name[p - name] = '\0';
  332.  
  333.   /* Skip over the whitespace after dev_name */
  334.   for (; isspace (*p); p++)
  335.     /*EMPTY*/;
  336.   
  337.   if (1 != sscanf (p, "%d ", &baudrate))
  338.     goto erroid;
  339.  
  340.   /* Skip the number and then the spaces */
  341.   for (; isdigit (*p); p++)
  342.     /*EMPTY*/;
  343.   for (; isspace (*p); p++)
  344.     /*EMPTY*/;
  345.   
  346.   if (prog_name != NULL)
  347.     free (prog_name);
  348.   prog_name = savestring (p, strlen (p));
  349.  
  350.   eb_close (0);
  351.  
  352.   eb_desc = open (dev_name, O_RDWR);
  353.   if (eb_desc < 0)
  354.     perror_with_name (dev_name);
  355.   ioctl (eb_desc, TIOCGETP, &sg);
  356. #ifdef HAVE_TERMIO
  357.   sg.c_cc[VMIN] = 0;        /* read with timeout.  */
  358.   sg.c_cc[VTIME] = timeout * 10;
  359.   sg.c_lflag &= ~(ICANON | ECHO);
  360.   sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
  361. #else
  362.   sg.sg_ispeed = damn_b (baudrate);
  363.   sg.sg_ospeed = damn_b (baudrate);
  364.   sg.sg_flags |= RAW | ANYP;
  365.   sg.sg_flags &= ~ECHO;
  366. #endif
  367.  
  368.   ioctl (eb_desc, TIOCSETP, &sg);
  369.   eb_stream = fdopen (eb_desc, "r+");
  370.  
  371.   push_target (&eb_ops);
  372.   if (from_tty)
  373.     printf ("Remote %s debugging %s using %s\n", target_shortname,
  374.         prog_name, dev_name);
  375.  
  376. #ifndef HAVE_TERMIO
  377. #ifndef NO_SIGINTERRUPT
  378.   /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
  379.      the read.  */
  380.   if (siginterrupt (SIGALRM, 1) != 0)
  381.     perror ("eb_open: error in siginterrupt");
  382. #endif
  383.  
  384.   /* Set up read timeout timer.  */
  385.   if ((void (*)) signal (SIGALRM, eb_timer) == (void (*)) -1)
  386.     perror ("eb_open: error in signal");
  387. #endif
  388.  
  389. #if defined (LOG_FILE)
  390.   log_file = fopen (LOG_FILE, "w");
  391.   if (log_file == NULL)
  392.     perror_with_name (LOG_FILE);
  393. #endif
  394.  
  395.   /* Hello?  Are you there?  */
  396.   write (eb_desc, "\n", 1);
  397.   
  398.   expect_prompt ();
  399. }
  400.  
  401. /* Close out all files and local state before this target loses control. */
  402.  
  403. void
  404. eb_close (quitting)
  405.      int quitting;
  406. {
  407.  
  408.   /* Due to a bug in Unix, fclose closes not only the stdio stream,
  409.      but also the file descriptor.  So we don't actually close
  410.      eb_desc.  */
  411.   if (eb_stream)
  412.     fclose (eb_stream);    /* This also closes eb_desc */
  413.   if (eb_desc >= 0)
  414.     /* close (eb_desc); */
  415.  
  416.   /* Do not try to close eb_desc again, later in the program.  */
  417.   eb_stream = NULL;
  418.   eb_desc = -1;
  419.  
  420. #if defined (LOG_FILE)
  421.   if (ferror (log_file))
  422.     printf ("Error writing log file.\n");
  423.   if (fclose (log_file) != 0)
  424.     printf ("Error closing log file.\n");
  425. #endif
  426. }
  427.  
  428. /* Terminate the open connection to the remote debugger.
  429.    Use this when you want to detach and do something else
  430.    with your gdb.  */
  431. void
  432. eb_detach (from_tty)
  433.      int from_tty;
  434. {
  435.   pop_target();        /* calls eb_close to do the real work */
  436.   if (from_tty)
  437.     printf ("Ending remote %s debugging\n", target_shortname);
  438. }
  439.  
  440. /* Tell the remote machine to resume.  */
  441.  
  442. void
  443. eb_resume (step, sig)
  444.      int step, sig;
  445. {
  446.   if (step)
  447.     {
  448.       write (eb_desc, "t 1,s\n", 6);
  449.       /* Wait for the echo.  */
  450.       expect ("t 1,s\r");
  451.       /* Then comes a line containing the instruction we stepped to.  */
  452.       expect ("\n@");
  453.       /* Then we get the prompt.  */
  454.       expect_prompt ();
  455.  
  456.       /* Force the next eb_wait to return a trap.  Not doing anything
  457.          about I/O from the target means that the user has to type
  458.          "continue" to see any.  This should be fixed.  */
  459.       need_artificial_trap = 1;
  460.     }
  461.   else
  462.     {
  463.       if (need_gi)
  464.     {
  465.       need_gi = 0;
  466.       write (eb_desc, "gi\n", 3);
  467.       
  468.       /* Swallow the echo of "gi".  */
  469.       expect ("gi\r");
  470.     }
  471.       else
  472.     {
  473.       write (eb_desc, "GR\n", 3);
  474.       /* Swallow the echo.  */
  475.       expect ("GR\r");
  476.     }
  477.     }
  478. }
  479.  
  480. /* Wait until the remote machine stops, then return,
  481.    storing status in STATUS just as `wait' would.  */
  482.  
  483. int
  484. eb_wait (status)
  485.      WAITTYPE *status;
  486. {
  487.   /* Strings to look for.  '?' means match any single character.  
  488.      Note that with the algorithm we use, the initial character
  489.      of the string cannot recur in the string, or we will not
  490.      find some cases of the string in the input.  */
  491.   
  492.   static char bpt[] = "Invalid interrupt taken - #0x50 - ";
  493.   /* It would be tempting to look for "\n[__exit + 0x8]\n"
  494.      but that requires loading symbols with "yc i" and even if
  495.      we did do that we don't know that the file has symbols.  */
  496.   static char exitmsg[] = "\n@????????I    JMPTI     GR121,LR0";
  497.   char *bp = bpt;
  498.   char *ep = exitmsg;
  499.  
  500.   /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.  */
  501.   char swallowed[50];
  502.   /* Current position in swallowed.  */
  503.   char *swallowed_p = swallowed;
  504.  
  505.   int ch;
  506.   int ch_handled;
  507.  
  508.   int old_timeout = timeout;
  509.  
  510.   WSETEXIT ((*status), 0);
  511.  
  512.   if (need_artificial_trap != 0)
  513.     {
  514.       WSETSTOP ((*status), SIGTRAP);
  515.       need_artificial_trap--;
  516.       return 0;
  517.     }
  518.  
  519.   timeout = 0;        /* Don't time out -- user program is running. */
  520.   while (1)
  521.     {
  522.       ch_handled = 0;
  523.       ch = readchar ();
  524.       if (ch == *bp)
  525.     {
  526.       bp++;
  527.       if (*bp == '\0')
  528.         break;
  529.       ch_handled = 1;
  530.  
  531.       *swallowed_p++ = ch;
  532.     }
  533.       else
  534.     bp = bpt;
  535.  
  536.       if (ch == *ep || *ep == '?')
  537.     {
  538.       ep++;
  539.       if (*ep == '\0')
  540.         break;
  541.  
  542.       if (!ch_handled)
  543.         *swallowed_p++ = ch;
  544.       ch_handled = 1;
  545.     }
  546.       else
  547.     ep = exitmsg;
  548.  
  549.       if (!ch_handled)
  550.     {
  551.       char *p;
  552.  
  553.       /* Print out any characters which have been swallowed.  */
  554.       for (p = swallowed; p < swallowed_p; ++p)
  555.         putc (*p, stdout);
  556.       swallowed_p = swallowed;
  557.       
  558.       putc (ch, stdout);
  559.     }
  560.     }
  561.   expect_prompt ();
  562.   if (*bp== '\0')
  563.     WSETSTOP ((*status), SIGTRAP);
  564.   else
  565.     WSETEXIT ((*status), 0);
  566.   timeout = old_timeout;
  567.  
  568.   return 0;
  569. }
  570.  
  571. /* Return the name of register number REGNO
  572.    in the form input and output by EBMON.
  573.  
  574.    Returns a pointer to a static buffer containing the answer.  */
  575. static char *
  576. get_reg_name (regno)
  577.      int regno;
  578. {
  579.   static char buf[80];
  580.   if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
  581.     sprintf (buf, "GR%03d", regno - GR96_REGNUM + 96);
  582.   else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
  583.     sprintf (buf, "LR%03d", regno - LR0_REGNUM);
  584.   else if (regno == Q_REGNUM)
  585.     strcpy (buf, "SR131");
  586.   else if (regno >= BP_REGNUM && regno <= CR_REGNUM)
  587.     sprintf (buf, "SR%03d", regno - BP_REGNUM + 133);
  588.   else if (regno == ALU_REGNUM)
  589.     strcpy (buf, "SR132");
  590.   else if (regno >= IPC_REGNUM && regno <= IPB_REGNUM)
  591.     sprintf (buf, "SR%03d", regno - IPC_REGNUM + 128);
  592.   else if (regno >= VAB_REGNUM && regno <= LRU_REGNUM)
  593.     sprintf (buf, "SR%03d", regno - VAB_REGNUM);
  594.   else if (regno == GR1_REGNUM)
  595.     strcpy (buf, "GR001");
  596.   return buf;
  597. }
  598.  
  599. /* Read the remote registers into the block REGS.  */
  600.  
  601. static void
  602. eb_fetch_registers ()
  603. {
  604.   int reg_index;
  605.   int regnum_index;
  606.   char tempbuf[10];
  607.   int i;
  608.  
  609. #if 0
  610.   /* This should not be necessary, because one is supposed to read the
  611.      registers only when the inferior is stopped (at least with
  612.      ptrace() and why not make it the same for remote?).  */
  613.   /* ^A is the "normal character" used to make sure we are talking to EBMON
  614.      and not to the program being debugged.  */
  615.   write (eb_desc, "\001\n");
  616.   expect_prompt ();
  617. #endif
  618.  
  619.   write (eb_desc, "dw gr96,gr127\n", 14);
  620.   for (reg_index = 96, regnum_index = GR96_REGNUM;
  621.        reg_index < 128;
  622.        reg_index += 4, regnum_index += 4)
  623.     {
  624.       sprintf (tempbuf, "GR%03d ", reg_index);
  625.       expect (tempbuf);
  626.       get_hex_regs (4, regnum_index);
  627.       expect ("\n");
  628.     }
  629.  
  630.   for (i = 0; i < 128; i += 32)
  631.     {
  632.       /* The PC has a tendency to hang if we get these
  633.      all in one fell swoop ("dw lr0,lr127").  */
  634.       sprintf (tempbuf, "dw lr%d\n", i);
  635.       write (eb_desc, tempbuf, strlen (tempbuf));
  636.       for (reg_index = i, regnum_index = LR0_REGNUM + i;
  637.        reg_index < i + 32;
  638.        reg_index += 4, regnum_index += 4)
  639.     {
  640.       sprintf (tempbuf, "LR%03d ", reg_index);
  641.       expect (tempbuf);
  642.       get_hex_regs (4, regnum_index);
  643.       expect ("\n");
  644.     }
  645.     }
  646.  
  647.   write (eb_desc, "dw sr133,sr133\n", 15);
  648.   expect ("SR133          ");
  649.   get_hex_regs (1, BP_REGNUM);
  650.   expect ("\n");
  651.  
  652.   write (eb_desc, "dw sr134,sr134\n", 15);
  653.   expect ("SR134                   ");
  654.   get_hex_regs (1, FC_REGNUM);
  655.   expect ("\n");
  656.  
  657.   write (eb_desc, "dw sr135,sr135\n", 15);
  658.   expect ("SR135                            ");
  659.   get_hex_regs (1, CR_REGNUM);
  660.   expect ("\n");
  661.  
  662.   write (eb_desc, "dw sr131,sr131\n", 15);
  663.   expect ("SR131                            ");
  664.   get_hex_regs (1, Q_REGNUM);
  665.   expect ("\n");
  666.  
  667.   write (eb_desc, "dw sr0,sr14\n", 12);
  668.   for (reg_index = 0, regnum_index = VAB_REGNUM;
  669.        regnum_index <= LRU_REGNUM;
  670.        regnum_index += 4, reg_index += 4)
  671.     {
  672.       sprintf (tempbuf, "SR%03d ", reg_index);
  673.       expect (tempbuf);
  674.       get_hex_regs (reg_index == 12 ? 3 : 4, regnum_index);
  675.       expect ("\n");
  676.     }
  677.  
  678.   /* There doesn't seem to be any way to get these.  */
  679.   {
  680.     int val = -1;
  681.     supply_register (FPE_REGNUM, &val);
  682.     supply_register (INT_REGNUM, &val);
  683.     supply_register (FPS_REGNUM, &val);
  684.     supply_register (EXO_REGNUM, &val);
  685.   }
  686.  
  687.   write (eb_desc, "dw gr1,gr1\n", 11);
  688.   expect ("GR001 ");
  689.   get_hex_regs (1, GR1_REGNUM);
  690.   expect_prompt ();
  691. }
  692.  
  693. /* Fetch register REGNO, or all registers if REGNO is -1.
  694.    Returns errno value.  */
  695. int
  696. eb_fetch_register (regno)
  697.      int regno;
  698. {
  699.   if (regno == -1)
  700.     eb_fetch_registers ();
  701.   else
  702.     {
  703.       char *name = get_reg_name (regno);
  704.       fprintf (eb_stream, "dw %s,%s\n", name, name);
  705.       expect (name);
  706.       expect (" ");
  707.       get_hex_regs (1, regno);
  708.       expect_prompt ();
  709.     }
  710.   return 0;
  711. }
  712.  
  713. /* Store the remote registers from the contents of the block REGS.  */
  714.  
  715. static void
  716. eb_store_registers ()
  717. {
  718.   int i, j;
  719.   fprintf (eb_stream, "s gr1,%x\n", read_register (GR1_REGNUM));
  720.   expect_prompt ();
  721.  
  722.   for (j = 0; j < 32; j += 16)
  723.     {
  724.       fprintf (eb_stream, "s gr%d,", j + 96);
  725.       for (i = 0; i < 15; ++i)
  726.     fprintf (eb_stream, "%x,", read_register (GR96_REGNUM + j + i));
  727.       fprintf (eb_stream, "%x\n", read_register (GR96_REGNUM + j + 15));
  728.       expect_prompt ();
  729.     }
  730.  
  731.   for (j = 0; j < 128; j += 16)
  732.     {
  733.       fprintf (eb_stream, "s lr%d,", j);
  734.       for (i = 0; i < 15; ++i)
  735.     fprintf (eb_stream, "%x,", read_register (LR0_REGNUM + j + i));
  736.       fprintf (eb_stream, "%x\n", read_register (LR0_REGNUM + j + 15));
  737.       expect_prompt ();
  738.     }
  739.  
  740.   fprintf (eb_stream, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM),
  741.        read_register (FC_REGNUM), read_register (CR_REGNUM));
  742.   expect_prompt ();
  743.   fprintf (eb_stream, "s sr131,%x\n", read_register (Q_REGNUM));
  744.   expect_prompt ();
  745.   fprintf (eb_stream, "s sr0,");
  746.   for (i = 0; i < 11; ++i)
  747.     fprintf (eb_stream, "%x,", read_register (VAB_REGNUM + i));
  748.   fprintf (eb_stream, "%x\n", read_register (VAB_REGNUM + 11));
  749.   expect_prompt ();
  750. }
  751.  
  752. /* Store register REGNO, or all if REGNO == 0.
  753.    Return errno value.  */
  754. int
  755. eb_store_register (regno)
  756.      int regno;
  757. {
  758.   if (regno == -1)
  759.     eb_store_registers ();
  760.   else
  761.     {
  762.       char *name = get_reg_name (regno);
  763.       fprintf (eb_stream, "s %s,%x\n", name, read_register (regno));
  764.       /* Setting GR1 changes the numbers of all the locals, so
  765.      invalidate the register cache.  Do this *after* calling
  766.      read_register, because we want read_register to return the
  767.      value that write_register has just stuffed into the registers
  768.      array, not the value of the register fetched from the
  769.      inferior.  */
  770.       if (regno == GR1_REGNUM)
  771.     registers_changed ();
  772.       expect_prompt ();
  773.     }
  774.   return 0;
  775. }
  776.  
  777. /* Get ready to modify the registers array.  On machines which store
  778.    individual registers, this doesn't need to do anything.  On machines
  779.    which store all the registers in one fell swoop, this makes sure
  780.    that registers contains all the registers from the program being
  781.    debugged.  */
  782.  
  783. void
  784. eb_prepare_to_store ()
  785. {
  786.   /* Do nothing, since we can store individual regs */
  787. }
  788.  
  789. /* FIXME!  Merge these two.  */
  790. int
  791. eb_xfer_inferior_memory (memaddr, myaddr, len, write, target)
  792.      CORE_ADDR memaddr;
  793.      char *myaddr;
  794.      int len;
  795.      int write;
  796.      struct target_ops *target;        /* ignored */
  797. {
  798.   if (write)
  799.     return eb_write_inferior_memory (memaddr, myaddr, len);
  800.   else
  801.     return eb_write_inferior_memory (memaddr, myaddr, len);
  802. }
  803.  
  804. void
  805. eb_files_info ()
  806. {
  807.   printf ("\tAttached to %s at %d baud and running program %s.\n",
  808.       dev_name, baudrate, prog_name);
  809. }
  810.  
  811. /* Copy LEN bytes of data from debugger memory at MYADDR
  812.    to inferior's memory at MEMADDR.  Returns errno value.  */
  813. int
  814. eb_write_inferior_memory (memaddr, myaddr, len)
  815.      CORE_ADDR memaddr;
  816.      char *myaddr;
  817.      int len;
  818. {
  819.   int i;
  820.  
  821.   for (i = 0; i < len; i++)
  822.     {
  823.       if ((i % 16) == 0)
  824.     fprintf (eb_stream, "sb %x,", memaddr + i);
  825.       if ((i % 16) == 15 || i == len - 1)
  826.     {
  827.       fprintf (eb_stream, "%x\n", ((unsigned char *)myaddr)[i]);
  828.       expect_prompt ();
  829.     }
  830.       else
  831.     fprintf (eb_stream, "%x,", ((unsigned char *)myaddr)[i]);
  832.     }
  833.   return 0;
  834. }
  835.  
  836. /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
  837.    at debugger address MYADDR.  Returns errno value.  */
  838. int
  839. eb_read_inferior_memory(memaddr, myaddr, len)
  840.      CORE_ADDR memaddr;
  841.      char *myaddr;
  842.      int len;
  843. {
  844.   int i;
  845.  
  846.   /* Number of bytes read so far.  */
  847.   int count;
  848.  
  849.   /* Starting address of this pass.  */
  850.   unsigned long startaddr;
  851.  
  852.   /* Number of bytes to read in this pass.  */
  853.   int len_this_pass;
  854.  
  855.   /* Note that this code works correctly if startaddr is just less
  856.      than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
  857.      thing).  That is, something like
  858.      eb_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
  859.      works--it never adds len to memaddr and gets 0.  */
  860.   /* However, something like
  861.      eb_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
  862.      doesn't need to work.  Detect it and give up if there's an attempt
  863.      to do that.  */
  864.   if (((memaddr - 1) + len) < memaddr)
  865.     return EIO;
  866.   
  867.   startaddr = memaddr;
  868.   count = 0;
  869.   while (count < len)
  870.     {
  871.       len_this_pass = 16;
  872.       if ((startaddr % 16) != 0)
  873.     len_this_pass -= startaddr % 16;
  874.       if (len_this_pass > (len - count))
  875.     len_this_pass = (len - count);
  876.  
  877.       fprintf (eb_stream, "db %x,%x\n", startaddr,
  878.            (startaddr - 1) + len_this_pass);
  879.       expect ("\n");
  880.  
  881.       /* Look for 8 hex digits.  */
  882.       i = 0;
  883.       while (1)
  884.     {
  885.       if (isxdigit (readchar ()))
  886.         ++i;
  887.       else
  888.         {
  889.           expect_prompt ();
  890.           error ("Hex digit expected from remote system.");
  891.         }
  892.       if (i >= 8)
  893.         break;
  894.     }
  895.  
  896.       expect ("  ");
  897.  
  898.       for (i = 0; i < len_this_pass; i++)
  899.     get_hex_byte (&myaddr[count++]);
  900.  
  901.       expect_prompt ();
  902.  
  903.       startaddr += len_this_pass;
  904.     }
  905.   return 0;
  906. }
  907.  
  908. /* Define the target subroutine names */
  909.  
  910. struct target_ops eb_ops = {
  911.     "amd-eb", "Remote serial AMD EBMON target",
  912.     "Use a remote computer running EBMON connected by a serial line.\n\
  913. Arguments are the name of the device for the serial line,\n\
  914. the speed to connect at in bits per second, and the filename of the\n\
  915. executable as it exists on the remote computer.  For example,\n\
  916.         target amd-eb /dev/ttya 9600 demo",
  917.     eb_open, eb_close, 
  918.     0, eb_detach, eb_resume, eb_wait,
  919.     eb_fetch_register, eb_store_register,
  920.     eb_prepare_to_store, 0, 0,     /* conv_to, conv_from */
  921.     eb_xfer_inferior_memory, eb_files_info,
  922.     0, 0,    /* Breakpoints */
  923.     0, 0, 0, 0, 0,    /* Terminal handling */
  924.     0,     /* FIXME, kill */
  925.     0,    /* load */
  926.     call_function_by_hand,
  927.     0, /* lookup_symbol */
  928.     0, /* create_inferior FIXME, eb_start here or something? */
  929.     0, /* mourn_inferior FIXME */
  930.     process_stratum, 0, /* next */
  931.     1, 1, 1, 1, 1,    /* all mem, mem, stack, regs, exec */
  932.     0, 0,            /* Section pointers */
  933.     OPS_MAGIC,        /* Always the last thing */
  934. };
  935.  
  936. void
  937. _initialize_remote_eb ()
  938. {
  939.   add_target (&eb_ops);
  940. }
  941.